package com.wenqiner.requesttool.view;

import com.wenqiner.requesttool.core.fx.AlertTool;
import com.wenqiner.requesttool.domain.HttpConstant;
import javafx.fxml.FXML;
import javafx.scene.control.CheckBox;
import javafx.scene.control.TextField;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author ranhaifeng
 */
public class HttpConfigurationController {
    public static final Logger log = LoggerFactory.getLogger(HttpConfigurationController.class);

    @FXML
    protected TextField connectTimeout;
    @FXML
    protected TextField readTimeout;
    @FXML
    protected CheckBox keepalive;
    @FXML
    protected TextField maxConnections;
    @FXML
    protected TextField maxKeepaliveRequests;


    @FXML
    protected void connectTimeoutChange() {
        String text = connectTimeout.getText();
        log.info("配置connectTimeout,值:[{}]", text);
        boolean condation = true;
        int v = 0;
        try {
            v = Integer.parseInt(text);
            if (Integer.parseInt(text) > 0 && Integer.parseInt(text) <= 1800) {
                condation = false;
            }
        } catch (NumberFormatException e) {
            log.error("配置connectTimeout的值不是有效数字");
        }
        if (condation) {
            AlertTool.showError("connectTimeout的值为1-1800 (秒)");
            return;
        }
        HttpConstant.setConnectionTimeout(v * 1000);
    }

    @FXML
    protected void readTimeoutChange() {
        String text = readTimeout.getText();
        log.info("配置readTimeout,值:[{}]", text);
        boolean condation = true;
        int v = 0;
        try {
            v = Integer.parseInt(text);
            if (Integer.parseInt(text) > 0 && Integer.parseInt(text) <= 1800) {
                condation = false;
            }
        } catch (NumberFormatException e) {
            log.error("配置readTimeout的值不是有效数字");
        }
        if (condation) {
            AlertTool.showError("readTimeout的值为1-1800 (秒)");
            return;
        }
        HttpConstant.setReadTimeout(v * 1000);
    }

    @FXML
    protected void keepaliveChange() {
        System.out.println(keepalive.isSelected());

    }

    @FXML
    protected void maxConnectionsChange() {
        String text = maxConnections.getText();
        log.info("配置maxConnections,值:[{}]", text);
        boolean condation = true;
        int v = 0;
        try {
            v = Integer.parseInt(text);
            if (Integer.parseInt(text) > 0 && Integer.parseInt(text) <= 500) {
                condation = false;
            }
        } catch (NumberFormatException e) {
            log.error("配置maxConnections的值不是有效数字");
        }
        if (condation) {
            AlertTool.showError("maxConnections的值为1-500");
            return;
        }
        HttpConstant.setDefaultMaxConnections(text);
        System.setProperty("http.maxConnections", HttpConstant.DEFAULT_MAX_CONNECTIONS);
    }

    @FXML
    protected void maxKeepaliveChange() {
        String text = maxKeepaliveRequests.getText();
        log.info("配置maxKeepaliveRequests,值:[{}]", text);
        boolean condation = true;
        int v = 0;
        try {
            v = Integer.parseInt(text);
            if (Integer.parseInt(text) >= -1 && Integer.parseInt(text) <= 100) {
                condation = false;
            }
        } catch (NumberFormatException e) {
            log.error("配置maxKeepaliveRequests的值不是有效数字");
        }
        if (condation) {
            AlertTool.showError("maxKeepaliveRequests的值为-1-100");
            return;
        }
        HttpConstant.setMaxKeepaliveRequests(text);
        System.setProperty("maxKeepAliveRequests", HttpConstant.MAX_KEEPALIVE_REQUESTS);
    }


}